<?php
/* 
* @Title:  [货品列表模型类]
* @Author: {chenlei} [chenleib5@126.com]
* @Date:   2014-04-08 19:20:44
* @Last Modified by:   Administrator
* @Last Modified time: 2014-04-27 22:15:18
* @Copyright:  [hn7m.com]
*/
class CombineModel extends Model{
	public $table='combine';
	//自动验证
	public $validate=array(
		//array('bcontent[1]','regexp:/^[1-9]\d*?$/','请选择颜色',2,3),
		array('binventory','regexp:/^\d+?$/','库存数必填,最小值0',2,3)
		);
	/**
	 * [add_data 添加数据]
	 */
	public function add_data(){
		//组合属性id到数据库表中bcontent字段中
		$attr=is_array(Q('post.bcontent'))?implode(',', Q('post.bcontent')):NULL;

		//判断是否重复添加规格组合
		$gid=Q('post.gid',0,'intval');
		$allData=$this->where(array('qm_goods_gid'=>$gid))->findAll();
		$temp=array();
		if(!empty($allData)){
			foreach ($allData as $key => $value) {
				$temp[]=$value['bcontent'];
			}
		}
		if(in_array($attr, $temp)){
			$this->error='该规格组合已存在,不能重复添加!';
			return false;
		}
		//组合要添加的数据
		$data=array(
			'bcontent'		=>$attr,
			'binventory'	=>Q('binventory', 0, 'intval'),
			'qm_goods_gid'	=>$gid
			);
		$this->add($data);  //执行添加

		$total=$this->where(array('qm_goods_gid'=>$gid))->sum('binventory');//计算该商品的总库存
		K('Goods')->update_inventory($gid,$total); //将总库存写入到goods表
		return true;
	}
	/**
	 * [get_data 获得所有的数据]
	 * @return [type] [description]
	 */
	public function get_data($where=NULL){
		return $this->where($where)->findAll();
	}
	/**
	 * [get_onedate_value 通过bid获得一条数据的字段值]
	 * @param  [type] $gid  [description]
	 * @param  [type] $need [description]
	 * @return [type]       [description]
	 */
	public function get_onedate_value($bid,$need){
		return $this->where(array('bid'=>$bid))->getField($need);
	}
	/**
	 * [get_one_value 通过gid,bcontent获得一条数据]
	 * @param  [type] $gid  [description]
	 * @param  [type] $need [description]
	 * @return [type]       [description]
	 */
	public function get_one_value($gid,$bcontent){
		return $this->where(array('qm_goods_gid'=>$gid,'bcontent'=>$bcontent))->find();
	}
	/**
	 * [get_combineSize 获得指定商品的尺寸组合]
	 * @param  [type] $gid [description]
	 * @return [type]      [description]
	 */
	public function get_combineSize($gid){
		$combine=$this->get_data(array('qm_goods_gid'=>$gid));  //获得该商品下所有的货品列表
		//重新组合其中的'组合属性bcontent'字段
		$temp=array();
		foreach ($combine as $key => $value) {
			$temp[]=$value['bcontent'];
		}
		foreach ($temp as $key => $value) {
			$temp[$key]=explode(',', $value);
		}
		foreach ($temp as $key => $value) {
			$combineSize[$value[0]]=$value[0];
		}
		$combineSize=array_unique($combineSize);
		foreach ($combineSize as $m => $n) {
			$str=K('Attr')->get_onedate_value($n,'acontent');
			$combineSize[$m]=$str;
		}
		return $combineSize;
	}
	/**
	 * [get_combineColor 获得指定商品的颜色组合]
	 * @param  [type] $gid [description]
	 * @return [type]      [description]
	 */
	public function get_combineColor($gid){
		$combine=$this->get_data(array('qm_goods_gid'=>$gid));  //获得该商品下所有的货品列表
		//重新组合其中的'组合属性bcontent'字段
		$temp=array();
		foreach ($combine as $key => $value) {
			$temp[]=$value['bcontent'];
		}
		foreach ($temp as $key => $value) {
			$temp[$key]=explode(',', $value);
		}
		foreach ($temp as $key => $value) {
			$combineColor[$value[1]]=$value[1];
		}
		$combineColor=array_unique($combineColor);
		foreach ($combineColor as $m => $n) {
			$str=K('Attr')->get_onedate_value($n,'acontent');
			$combineColor[$m]=$str;
		}
		return $combineColor;
	}
	/**
	 * [del_data 删除数据]
	 * @return [type]      [description]
	 */
	public function del_data($bid){
		$gid=$this->where(array('bid'=>$bid))->getField('qm_goods_gid');
		if($this->del($bid)){
			$total=$this->where(array('qm_goods_gid'=>$gid))->sum('binventory');//计算该商品的总库存
			K('Goods')->update_inventory($gid,$total); //将总库存写入到goods表
			return true;
		}
	}
	/**
	 * [save_data 修改库存]
	 * @return [type]      [description]
	 */
	public function save_data($bid,$data){
		$gid=$this->where(array('bid'=>$bid))->getField('qm_goods_gid');
		if($this->where(array('bid'=>$bid))->update($data)){
			$total=$this->where(array('qm_goods_gid'=>$gid))->sum('binventory');//计算该商品的总库存
			K('Goods')->update_inventory($gid,$total); //将总库存写入到goods表
			return true;
		}
	}
	/**
	 * [switch_bcontent 转换组合属性id变为文字]
	 * @return [type] [description]
	 */
	public function switch_bcontent($bid){
		$temp=$this->where(array('bid'=>$bid))->getField('bcontent');
		$temp=explode(',', $temp);
		foreach ($temp as $key => $value) {
			$temp[$key]=K('Attr')->get_onedate_value($value,'acontent');
		}
		$temp=implode('， ', $temp);
		return $temp;
	}
	/**
	 * [del_combine 按gid删除货品列表]
	 * @param  [type] $gid [description]
	 * @return [type]      [description]
	 */
	public function del_combine($gid){
		return $this->where(array('qm_goods_gid'=>$gid))->del();
	}
}
?>